package main

import (
	"fmt"
	"net/http"
	"regexp"
	"strconv"
)




func HttpGet(url  string)(result string, err error) {
	resp, err1 := http.Get(url)
	if err != nil {
		err = err1
		return
	}

	defer resp.Body.Close()
	//读取网页body
	buf := make([] byte, 1024*4)
	for{
		n, _ := resp.Body.Read(buf)

		if  n == 0 {
			//
			break

		}
    	result += string(buf[:n])
    }
    return
}
//开始爬取每一个笑话，每一个段子 title, content, err:= SpiderOneJoy(url)
func SpiderOneJoy(url string) (title, content string, err error)  {
	result,err := HttpGet(url)
	if err != nil{
		err = err
		return
	}
	//取关键信息
	//取标题 <h1> 标题 </h1> 只取

	rel :=  regexp.MustCompile(`<h1>(?s:(.*?))</h1>`)
	if rel == nil{

		err = fmt.Errorf("%s","regexp.MustCompile err")
	return
	}
	 //取内容
	tmpTitle := rel.FindAllStringSubmatch(result, 1)
	//迭代
	for _, data := range tmpTitle{
		title = data[1]
		break
	}
	//取标题 取内容
	rel2 :=  regexp.MustCompile(`<div class="compile-txt py 10">(?s:(.*?))<a id ="prev" href="`)
	if rel2 == nil{

		err = fmt.Errorf("%s","regexp.MustCompile err2")
		return
	}
	//取内容
	tmpContent := rel2.FindAllStringSubmatch(result, 1)
	//迭代
	for _, data := range tmpContent{
		content = data[1]
		break
	}

	return



}



func SpiderPape(i  int)  {
	//明确爬取的url
    url :="https://www.pengfu.com/xiaohua_" + strconv.Itoa(i) +".html"

    fmt.Println("正在爬去第%d个网页：%s是\n", i, url)
    //开始爬取页面内容

    result, err  := HttpGet(url)
     if err != nil{
     	fmt.Println("HttpGet err = ", err)
		 return
	}
   // fmt.Println("r = ", result)
   //, <h1 class="dp-b"><a href=" 一个段子url连接
   //解释表达式

   re := regexp.MustCompile(`<h1 class="dp-b"><a href="(?s:(.*?))"`)
   if re ==nil{
   	//fmt.Println("regexp.MustComplie err")
	   return


   }
   //取关键信息
   joyUrls := re.FindAllStringSubmatch(result, -1)
   fmt.Println("joyUrls =", joyUrls)
   //取网址
   //第一个返回下标，第二个返回内容
	for _,  data := range joyUrls{
		//fmt.Println("url = ", data[1])
		title, content, err:= SpiderOneJoy(data[1])
		if err != nil{
			fmt.Println("SpiderOneJoy err = ",err)
			continue
		}
		fmt.Println("title = ", title)
		fmt.Println("content",content)
	}


}




func DoWork(start, end  int){
	fmt.Printf("准备爬去第%d页到%d的网址\n",start,end)

	  for i := start; i <= end; i++{
	  	//定义一个函数，爬去主页面
	  	SpiderPape(i)
	  }
}




func main ()  {
	var start ,end int
	fmt.Println("请输入起始页（>= 1）:")
	fmt.Scan(&start)
	fmt.Printf("请输入终止页（>=起始页）:")
	fmt.Scan(&end)

	DoWork(start,end)

}